import 'package:efood_multivendor/controller/cart_controller.dart';
import 'package:efood_multivendor/controller/restaurant_controller.dart';
import 'package:efood_multivendor/data/model/response/cart_model.dart';
import 'package:efood_multivendor/data/model/response/restaurant_model.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/quantity_button.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../helper/date_converter.dart';
import '../../../../helper/price_converter.dart';
import '../../../../helper/route_helper.dart';
import '../../../../util/dimensions.dart';
import '../../../../util/images.dart';
import '../../../base/confirmation_dialog.dart';
import '../../../base/custom_image.dart';
import '../../product/product_screen.dart';

class CartListItem extends StatefulWidget {
  int index;
  bool fromRes;

  CartListItem({Key? key, required this.index, required this.fromRes})
      : super(key: key);

  @override
  State<CartListItem> createState() => _CartListItemState();
}

class _CartListItemState extends State<CartListItem> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<CartController>(builder: (cartController) {
      CartModel me = cartController.cartList[widget.index];
      double? _discount = me.product?.discountedItemPrice;
      bool _isAvailable = false;

      // String _discountType;
      // _discount = (me.product.restaurantDiscount == 0 || me.isCampaign)
      //     ? me.product.discount
      //     : me.product.restaurantDiscount;
      // _discountType = (me.product.restaurantDiscount == 0 || me.isCampaign)
      //     ? me.product.discountType
      //     : 'percent';
      _isAvailable =
          me.product?.is_open == true && me.product?.restaurantTooFar != true;

      return Padding(
        padding: EdgeInsets.symmetric(vertical: 2.h, horizontal: 3.w),
        child: InkWell(
            child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(Dimensions.RADIUS_SMALL),
              child: CustomImage(
                image: me.product?.image ?? '',
                height: 8.h,
                width: 8.h,
                fit: BoxFit.cover,
              ),
            ),
            SizedBox(width: Dimensions.PADDING_SIZE_SMALL),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  width: 30.w,
                  child: Text(
                    me.product?.name ?? '',
                    style: sfRegular.copyWith(
                      fontSize: Dimensions.fontSizeLarge,
                    ),
                    maxLines: 3,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Row(children: [
                  Text(
                    PriceConverter.convertPrice(me.product!.price!,
                        discount: _discount, discountType: "amount"),
                    style:
                        sfMedium.copyWith(fontSize: Dimensions.fontSizeLarge),
                  ),
                  SizedBox(
                      width: _discount! > 0
                          ? Dimensions.PADDING_SIZE_EXTRA_SMALL
                          : 0),
                  _discount > 0
                      ? Text(
                          PriceConverter.convertPrice(me.product!.price!),
                          style: sfMedium.copyWith(
                            fontSize: Dimensions.fontSizeExtraSmall,
                            color: Theme.of(context).disabledColor,
                            decoration: TextDecoration.lineThrough,
                            decorationColor: Theme.of(context).disabledColor,
                          ),
                        )
                      : SizedBox(),
                ]),
              ],
            ),
            Spacer(),
            QuantityButton(
              isActive: true,
              isIncrement: false,
              onTap: () async {
                if (widget.fromRes) {
                  if (cartController.getTotalQuantity(
                          resId: me.product!.restaurantId) >
                      1) {
                    cartController.removeFromCart(me, widget.fromRes);
                  } else {
                    Get.back();
                    await Future.delayed(Duration(milliseconds: 10));
                    cartController.removeFromCart(me, widget.fromRes);
                  }
                } else {
                  if (cartController.getTotalQuantity() > 1) {
                    cartController.removeFromCart(me, widget.fromRes);
                  } else {
                    Get.back();
                    await Future.delayed(Duration(milliseconds: 10));
                    cartController.removeFromCart(me, widget.fromRes);
                  }
                }
              },
            ),
            Text(cartController.getQuantity(me.product).toString(),
                style:
                    sfMedium.copyWith(fontSize: Dimensions.fontSizeExtraLarge)),
            QuantityButton(
                isActive: true,
                isIncrement: true,
                onTap: () {
                  if (Get.find<CartController>().existAnotherRestaurantProduct(
                      me.product!.restaurantId!)) {
                    Get.find<CartController>().addToCart(me);
                  } else {
                    Get.find<CartController>().addToCart(me);
                  }
                })
          ],
        )),
      );
    });
  }
}
